package com.tc.converter;

import com.google.common.base.Converter;
import com.tc.entity.TestConverterBean1;
import com.tc.entity.TestConverterBean2;
import com.tc.entity.TestMapperBean1;
import com.tc.entity.TestMapperBean2;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class GuavaConverter extends Converter<TestConverterBean1, TestConverterBean2> implements Supportable<String> {
    @Override
    protected TestConverterBean2 doForward(TestConverterBean1 testConverterBean1) {
        TestConverterBean2 testConverterBean2 = new TestConverterBean2();
        testConverterBean2.setName(testConverterBean1.getName());
        testConverterBean2.setAge(testConverterBean1.getAge());
        List<TestMapperBean1> amount1 = testConverterBean1.getAmount1();
        List<TestMapperBean2> amount2 = new ArrayList<>();
        if(amount1!=null){
            for (TestMapperBean1 amount:amount1) {
                TestMapperBean2 testMapperBean2 = new TestMapperBean2();
                testMapperBean2.setAmount2(amount.getAmount2());
                testMapperBean2.setAmount( new BigDecimal(amount.getAmount()).divide(new BigDecimal(100)));
                amount2.add(testMapperBean2);
            }
        }
        testConverterBean2.setAmount2(amount2);
        return testConverterBean2;
    }

    @Override
    protected TestConverterBean1 doBackward(TestConverterBean2 testConverterBean2) {
        TestConverterBean1 testConverterBean1 = new TestConverterBean1();
        testConverterBean1.setName(testConverterBean2.getName());
        testConverterBean1.setAge(testConverterBean2.getAge());
        List<TestMapperBean2> amount2 = testConverterBean2.getAmount2();
        List<TestMapperBean1> amount1 = new ArrayList<>();
        if(amount2!=null){
            for (TestMapperBean2 amount:amount2) {
                TestMapperBean1 testMapperBean2 = new TestMapperBean1();
                testMapperBean2.setAmount2(amount.getAmount2());
                testMapperBean2.setAmount(amount.getAmount().longValue());
                amount1.add(testMapperBean2);
            }
        }
        testConverterBean1.setAmount1(amount1);
        return testConverterBean1;
    }

    @Override
    public String[] supports() {
        return new String[]{"myType"};
    }

}
